﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaseUnits;

namespace Carbon_Tracker
{
    public partial class frmPowerPlantManager : Form
    {
        private bool modifyMode = false;
        public frmPowerPlantManager()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IPowerPlant newPowerPlant;
                String externalId = this.txtExternalID.Text;
                String description = this.txtDescription.Text;
                int powerPlantId;
                BaseUnits.Builders.PowerPlantBuilder builder = new BaseUnits.Builders.PowerPlantBuilder(new DatabaseInterface.Connection());
                if (this.txtID.Text == "")
                {
                    powerPlantId = -1;
                }
                else
                {
                    powerPlantId = Convert.ToInt32(this.txtID.Text);
                }

                newPowerPlant = new BaseUnits.PowerPlant(powerPlantId, externalId, description, null);
                int newPowerUnitId = builder.PersistPowerPlant(newPowerPlant);

                if (powerPlantId == -1)
                {
                    newPowerPlant.PowerPlantId = newPowerUnitId;
                }

                if (modifyMode)
                {
                    MessageBox.Show("Power plant successfully modified", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("New power plant created successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
